package humanMirror.model.main;


import humanMirror.model.controller.Controller;
import humanMirror.model.controller.ControllerInterface;
import humanMirror.model.human.Human;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class GraphInt extends JApplet {

	private static final long serialVersionUID = 1422279990467588951L;
	private JList list;
	private JLabel question;
	private ControllerInterface controllerInterface;
	private int currentQuestion;

	private JButton btnNewButton;
	private JButton btnExit;

	public void init() {
		Human human = new Human();
		controllerInterface = new Controller(human);
		currentQuestion = 0;
		list = new JList();

		this.setSize(1024, 400);
		btnNewButton = new JButton("Next");

		btnNewButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {

				controllerInterface.setAnswers(list.getSelectedIndices());
				currentQuestion++;
				stopper();
			}

			private void stopper() {
				if (currentQuestion == controllerInterface.getCountOfQuestions()) {
					controllerInterface.stopAsking();
					JOptionPane.showMessageDialog(getRootPane(),
							controllerInterface.getRecommendationArray(),"Result",JOptionPane.INFORMATION_MESSAGE);
					System.exit(0);
				} else
					windowFill();
			}
		});
		
		btnExit = new JButton("Exit");
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		question = new JLabel();
		draw();
		windowFill();

	}

	private void windowFill() {
		question.setText(controllerInterface.getQuestion(currentQuestion));
		list.setListData(controllerInterface.getAnswers());
	}

	private void draw() {
		getContentPane().setEnabled(false);
		getContentPane().setLayout(null);
		list.setBounds(2, 33, 1020, 222);
		getContentPane().add(list);
		btnNewButton.setBounds(300, 262, 97, 25);
		getContentPane().add(btnNewButton);
		btnExit.setBounds(450, 262, 97, 25);
		getContentPane().add(btnExit);
		question.setBounds(300, 4, 500, 16);
		getContentPane().add(question);

	}

}
